package com.xd.core.business.consumer;

import com.rabbitmq.client.Channel;
import com.xd.core.business.service.IIncomingInfoService;
import com.xd.core.debt.dto.Review;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
@RabbitListener(queues = "delay.queueCheckIncoming",ackMode = "MANUAL")
public class CheckIncomingConsumer {
    @Autowired
    private IIncomingInfoService iIncomingInfoService;

    //1秒后 自动审核未通过的进件
    @RabbitHandler
    public void AutoCheckIncoming(Review review, Channel channel, Message message) throws IOException {
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("开始执行自动审核未通过进件");
            iIncomingInfoService.platformReview(review);
            log.info("自动审核未通过进件执行结束");
            channel.basicAck(tag, false); // 手动确认消息
        }catch (Exception e){
            log.error("处理进件审核失败，消息将拒绝：{}", review, e);
            channel.basicNack(tag, false, false); // 拒绝消息，不重回队列
        }

    }
}
